import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

class Map
{
	private JPanel ctnr = new JPanel();
	private ArrayList<MyButton> cells = new ArrayList<MyButton>();
	private MyEventHandler evtHdlr;
	
	private int[] margin = {0, 0};
	
	public Map()
	{
		setContainer(0, 0);
		make();
	}
	
	public Map(int marginX, int marginY)
	{
		margin[0] = marginX;
		margin[1] = marginY;
		
		setContainer(marginX, marginY);
		make();
	}
	
	public JPanel getCtnr()
	{
		return ctnr;
	}
	
	public void render(JComponent parentCtnr)
	{
		parentCtnr.add(ctnr);
	}
	
	private void setContainer(int marginX, int marginY)
	{
		JFrame window = GUI.window;
		
		ctnr.setBounds(marginX, marginY, 400, 400);
		
		ctnr.setLayout(null);
	}
	
	public void enableWarMode()
	{
		block();
	
		for (MyButton cell : cells)
		{
			if (!cell.isBlocked())
			{
				cell.addActionListener(new MyEventHandler("shooting"));
			}
		}
	}
	
	public void enableLocatingMode()
	{
		block();
	
		for (MyButton cell : cells)
		{
			if (!cell.isBlocked())
			{
				cell.addActionListener(new MyEventHandler("locating"));
			}
		}
	}
	
	public void block()
	{
		for (MyButton cell : cells)
		{
			cell.removeAL();
		}
	}
	
	public void reloadAreaCells()
	{
		ctnr.removeAll();
		ctnr.repaint();
		cells = new ArrayList<MyButton>();
		make();
	}
	
	public void locateShips(int[][] area)
	{	
		reloadAreaCells();
	
		int cellNumber;
	
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				if (area[i][j] == 1)
				{ 
					cellNumber = i * 10 + j;
					MyButton btn = getButtonByCoords(i, j);
					btn.occupy();
				}
			}
		}
		
		//GUI.window.validate();
	}
	
	public void actionPerformed(ActionEvent event)
	{
		System.out.println("Click!");
	}
	
	private void make()
	{
		Insets insets = ctnr.getInsets();
		int i;
		int areaXCoord = 0;
		int areaYCoord = 0;
		int y    = 0;
		double x = 0.0;
		
		for (i = 0; i < 100; i++)
		{
			double x_400 = x / 400;
			areaXCoord = i - areaYCoord * 10;
			
			MyButton button = new MyButton();
			button.setPreferredSize(new Dimension(40, 40));
			button.setMargin(new Insets(0, 0, 0, 0));
			
			if (Math.round(x_400) == (x_400) && (x != 0))
			{
				y += 40;
				x = 0.0;
				
				areaYCoord++;
				areaXCoord = 0;
			}
			
			int xCoord = (int) x;
			
			button.setBounds(insets.left + xCoord, insets.top + y, 40, 40);
			button.setCoords(areaXCoord, areaYCoord);
			
			cells.add(button);
			ctnr.add(button);
			
			x += 40;
		}
		
		GUI.window.repaint();
	}
	
	public MyButton getButtonByCoord(String coord)
	{
		MyButton foundBtn = null;
		
		for (MyButton btn : cells)
		{
			int[] coords = btn.getCoords();
			int[] givenCoords = Tools.convertStringCoordsToInt(coord);
			
			if (givenCoords[0] == coords[0] && givenCoords[1] == coords[1]) 
			{
				foundBtn = btn;
				break;
			}
		}
		
		return foundBtn;
	}
	
	public MyButton getButtonByCoords(int coordX, int coordY)
	{
		MyButton foundBtn = null;
		
		for (MyButton btn : cells)
		{
			int[] coords = btn.getCoords();
			
			if (coords[0] == coordX && coords[1] == coordY) 
			{
				foundBtn = btn;
				break;
			}
		}
		
		return foundBtn;
	}
}